﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Srvtools;
using System.Windows.Forms;
using System.Drawing.Design;

namespace Srvtools
{
    public partial class InfoRefButton : System.Windows.Forms.Button
    {
        public InfoRefButton()
        {
            _refButtonMatchs = new RefButtonMatchs(this, typeof(RefButtonMatch));
            _columns = new RefColumnsCollection(this, typeof(RefColumns));
            InitializeComponent();
        }

        public InfoRefButton(IContainer container)
        {
            container.Add(this);
            _refButtonMatchs = new RefButtonMatchs(this, typeof(RefButtonMatch));
            _columns = new RefColumnsCollection(this, typeof(RefColumns));
            InitializeComponent();
        }

        private Panel _panel;
        public Panel panel
        {
            get { return _panel; }
            set { _panel = value; }
        }

        private InfoTranslate _infoTranslate;
        public InfoTranslate infoTranslate
        {
            get { return _infoTranslate; }
            set { _infoTranslate = value; }
        }

        private RefButtonMatchs _refButtonMatchs;
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        //[Category("Infolight"),
        //Description("The columns which InfoTranslate is applied to")]
        public RefButtonMatchs refButtonMatchs
        {
            get
            {
                return _refButtonMatchs;
            }
            set
            {
                _refButtonMatchs = value;
            }
        }

        private bool _autoPanel = false;
        public bool autoPanel
        {
            get { return _autoPanel; }
            set { _autoPanel = value; }
        }

        private RefColumnsCollection _columns;
        [Category("Infolight"),
        Description("Specifies the columns to display on the form")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public RefColumnsCollection Columns
        {
            get { return _columns; }
            set { _columns = value; }
        }

        //add  MultiSelect by luciferling 20080730
        private bool _multiSelect = true;
        public bool multiSelect
        {
            get { return _multiSelect; }
            set { _multiSelect = value; }
        }

        private int formWidth = 600;

        public int FormWidth
        {
            get { return formWidth; }
            set { formWidth = value; }
        }

        private int formHeight = 350;

        public int FormHeight
        {
            get { return formHeight; }
            set { formHeight = value; }
        }

        InfoRefPanel innerPanel;
        protected override void OnClick(EventArgs e)
        {
            foreach (Control c in this.FindForm().Controls)
            {
                if (c is Panel || c is SplitterPanel || c is SplitContainer)
                {
                    if (FindNavigator(c) == false)
                    {
                        return;
                    }
                }
            }
            //add multiSelect by luciferling 20080730
            //当multiSelect 为 true autoPanel为 false 时，使user 设计的infodatagridview 设定成整行选择，方便复选行
            if (multiSelect & !autoPanel)
            {
                ((InfoDataGridView)this.panel.Controls[0]).SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }
            //end add
            innerPanel = new InfoRefPanel(this.panel, this.infoTranslate, this.autoPanel, this.multiSelect, Columns);//add multiSelect by luciferling 20080730
            innerPanel.AfterOK += new EventHandler<AfterOKEventArgs>(irp_AfterOK);
            innerPanel.Width = FormWidth;
            innerPanel.Height = FormHeight;
            innerPanel.ShowDialog();
            base.OnClick(e);
        }

        private bool FindNavigator(Control ctrl)
        {
            bool canedit = true;
            foreach (Control c in ctrl.Controls)
            {
                if (c is Panel || c is SplitterPanel || c is SplitContainer)
                {
                     canedit &= FindNavigator(c);
                }
                else
                {
                    foreach (RefButtonMatch rbm in this.refButtonMatchs)
                    {
                        if (rbm.Name == c.Name)
                        {
                            if (c.DataBindings.Count > 0 && c.DataBindings[0].DataSource is InfoBindingSource)
                            {
                                if ((c.DataBindings[0].DataSource as InfoBindingSource).BeginEdit() == false)
                                {
                                    canedit = false;
                                }
                            }
                        }
                    }
                }
            }
            return canedit;
        }

        void irp_AfterOK(object sender, AfterOKEventArgs e)
        {
            String[] strValue = new string[(sender as InfoRefPanel).value.Count];
            String[] strDisplay = new string[(sender as InfoRefPanel).display.Count];
            for (int i = 0; i < strValue.Length; i++)
            {
                strValue[i] = (sender as InfoRefPanel).value[i].ToString();
                strDisplay[i] = (sender as InfoRefPanel).display[i].ToString();
            }
            int num = 0;
            for (int j = 0; j < this.refButtonMatchs.Count; j++)
                for(int i = 0; i < this.Parent.Controls.Count; i++)
                    if ((refButtonMatchs[j] as RefButtonMatch).matchColumnName == this.Parent.Controls[i].Name)
                        if (num < strValue.Length)
                        {
                            if (this.Parent.Controls[i] is InfoRefbuttonBox)
                            {
                                (this.Parent.Controls[i] as InfoRefbuttonBox).Text = strDisplay[num];
                                (this.Parent.Controls[i] as InfoRefbuttonBox).RealValue = strValue[num];
                                (this.Parent.Controls[i] as InfoRefbuttonBox).Enter += new EventHandler(InfoRefButton_Enter);
                                (this.Parent.Controls[i] as InfoRefbuttonBox).Leave += new EventHandler(InfoRefButton_Leave);
                            }
                            else
                            {
                                this.Parent.Controls[i].Text = strValue[num];
                                if (this.Parent.Controls[i].GetType().GetInterface("IWriteValue", true) != null)
                                {
                                    ((IWriteValue)this.Parent.Controls[i]).WriteValue();
                                }
                            }
                            num++;
                            continue;
                        }
        }

        void InfoRefButton_Enter(object sender, EventArgs e)
        {
            (sender as InfoRefbuttonBox).Text = innerPanel.value[0].ToString();
        }

        void InfoRefButton_Leave(object sender, EventArgs e)
        {
            (sender as InfoRefbuttonBox).Text = innerPanel.display[0].ToString();
        }
    }

    public class RefButtonMatchs : InfoOwnerCollection
    {
        public RefButtonMatchs(Object aOwner, Type aItemType)
            : base(aOwner, typeof(RefButtonMatch))
        {

        }
        public new RefButtonMatch this[int index]
        {
            get
            {
                return (RefButtonMatch)InnerList[index];
            }
            set
            {
                if (index > -1 && index < Count)
                {
                    if (value is RefButtonMatch)
                    {
                        //原来的Collection设置为0
                        ((RefButtonMatch)InnerList[index]).Collection = null;
                        InnerList[index] = value;
                        //Collection设置为this
                        ((RefButtonMatch)InnerList[index]).Collection = this;
                    }
                }
            }
        }
    }

    public class RefButtonMatch : InfoOwnerCollectionItem, IGetValues
    {
        public RefButtonMatch()
        {
            _matchColumnName = "";
        }

        private string _name;
        [NotifyParentProperty(true)]
        public override string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        private string _matchColumnName;
        [Editor(typeof(FieldNameEditor), typeof(UITypeEditor))]
        [NotifyParentProperty(true)]
        public string matchColumnName
        {
            get
            {
                return _matchColumnName;
            }
            set
            {
                _matchColumnName = value;
                if (value != null && value != "")
                {
                    _name = value;
                }
            }
        }

        public string[] GetValues(string sKind)
        {
            List<string> values = new List<string>();
            if (this.Owner is InfoRefButton)
            {
                if (string.Compare(sKind, "matchcolumnname", true) == 0)//IgnoreCase
                {
                    for (int i = 0; i < (this.Owner as InfoRefButton).Parent.Controls.Count; i++)
                    {
                        if ((this.Owner as InfoRefButton).Parent.Controls[i] is InfoTextBox || (this.Owner as InfoRefButton).Parent.Controls[i] is TextBox
                            || (this.Owner as InfoRefButton).Parent.Controls[i] is DateTimePicker || (this.Owner as InfoRefButton).Parent.Controls[i] is InfoDateTimePicker
                            || (this.Owner as InfoRefButton).Parent.Controls[i] is InfoMaskedTextBox || (this.Owner as InfoRefButton).Parent.Controls[i] is InfoRefvalBox)
                        {
                            values.Add((this.Owner as InfoRefButton).Parent.Controls[i].Name);
                        }
                    }
                }
            }
            else if (this.Owner is InfoDataGridViewRefButtonColumn)
            {
                if (string.Compare(sKind, "matchcolumnname", true) == 0)
                {
                    DataGridViewColumnCollection columns = (this.Owner as DataGridViewColumn).DataGridView.Columns;
                    foreach (DataGridViewColumn column in columns)
                    {
                        if (column != this.Owner)
                        {
                            values.Add(column.Name);
                        }
                    }
                }
            }
            return values.ToArray();
        }
    }
}
